package com.wangjw.tuling.zookeeper.zkclient;

import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;

import java.util.List;

/**
 * Created by wangjianwen on 2018/12/6.
 */
public class ZkClientCrud<T> {

    private static final String CONNECT_URL = "47.96.129.248:2181,116.62.115.21:2181,47.96.224.44:2181";

    private ZkClient zkClient;

    public ZkClientCrud() {
        zkClient = new ZkClient(CONNECT_URL, 5000, 5000, new SerializableSerializer());
    }

    public void createPersistent(String path, Object data) {
        zkClient.createPersistent(path, data);
    }

    public T readData(String path) {
        return zkClient.readData(path);
    }

    public List<String> getChildren(String path) {
        return zkClient.getChildren(path);
    }

    public void writeData(String path, Object object) {
        zkClient.writeData(path, object);
    }

    public void deleteRecursive(String path) {
        zkClient.deleteRecursive(path);
    }

    // 递归创建
    public void createPersistent(String path, boolean createParents) {
        zkClient.createPersistent(path, createParents);
    }

    public static void main(String[] args) {
        ZkClientCrud zkClientCrud = new ZkClientCrud();
        User user = new User();
        user.setName("wangjw");
        user.setAge(31);
        zkClientCrud.createPersistent("/user", user);
        System.out.println(zkClientCrud.readData("/user"));
    }
}
